package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.QueryPageParam;
import com.example.demo.common.Result;
import com.example.demo.entity.Orders;
import com.example.demo.entity.User;
import com.example.demo.entity.Weifen;
import com.example.demo.entity.Yifenpei;
import com.example.demo.service.WeifenService;
import com.example.demo.service.YifenpeiService;
import com.example.demo.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/yifen")
public class YiFenController {
    @Autowired
    YifenpeiService yifenpeiService;

    @PostMapping
    public Result save(@RequestBody Yifenpei yifenpei){
        yifenpeiService.saveOrUpdate(yifenpei);
        return Result.suc();
    }

    @PostMapping("/status")
    public Result changeStatus(@RequestBody Yifenpei yifenpei) {
        yifenpeiService.saveOrUpdate(yifenpei);
        return Result.suc();
    }
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Weifen weifen) {
    Yifenpei yifenpei = new Yifenpei();
    yifenpei.setOrderno(weifen.getOrderno());
    yifenpei.setStaff(weifen.getStaff());
    yifenpei.setCustomer(weifen.getCustomer());
    yifenpei.setPhone(weifen.getPhone());
    yifenpei.setGoods(weifen.getGoods());
    yifenpei.setUnit(weifen.getUnit());
    yifenpei.setDanprice(weifen.getDanprice());
    yifenpei.setNum(weifen.getNum());
    yifenpei.setPrice(weifen.getPrice());
    yifenpei.setAddress(weifen.getAddress());
    yifenpei.setChuaddress(weifen.getChuaddress());
        yifenpei.setStatus(6);
        yifenpeiService.save(yifenpei);
        return Result.suc();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        yifenpeiService.removeById(id);
        return Result.suc();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        yifenpeiService.removeByIds(ids);
        return Result.suc();
    }

    @GetMapping("/page")
    public Result findPage(
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
       User user= TokenUtils.getCurrentUser();

        QueryWrapper<Yifenpei> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if(user.getRoleId()==1){
           queryWrapper.eq("staff",user.getName());
        }
        Page<Yifenpei> page = yifenpeiService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.suc(page);
    }
    @PostMapping("/listPageC1")
    public Result listPageC1(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String orderno = (String)param.get("orderno");
        Page<Yifenpei> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Yifenpei> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Yifenpei::getStaff,name);
        }
        if(StringUtils.isNotBlank(orderno)){
            lambdaQueryWrapper.eq(Yifenpei::getOrderno,orderno);
        }
        IPage result = yifenpeiService.pageCC(page,lambdaQueryWrapper);

        System.out.println("total=="+result.getTotal());

        return Result.suc(result.getRecords(),result.getTotal());
    }

}
